﻿using GraphLab;
using System.Text;

const string CONSOLE_TEXT = "E - add new edge, M - make new node, F - find minimum spanning tree, S - show graph, T - start test, N - exit";
string input;
const string TEST_FILE = "test3.txt";
Graph graph = new Graph();

string extractFile()
{
    FileStream file = new FileStream($"C:\\Users\\antrelax\\source\\repos\\GraphLab\\GraphLab\\{TEST_FILE}", FileMode.Open, FileAccess.Read);
    byte[] buffer = new byte[file.Length];
    file.Read(buffer, 0, buffer.Length);

    file.Close();

    string text = Encoding.UTF8.GetString(buffer);
    return text;
}

void tryAddEdge()
{
    Console.Write("Input begin node: ");
    string beginName = Console.ReadLine();
    Console.Write("Input end node: ");
    string endName = Console.ReadLine();
    Console.Write("Input edge weight: ");
    int weight = Convert.ToInt32(Console.ReadLine());

    try
    {
        graph.addEdge(beginName, endName, weight);
        Console.WriteLine("Success!");
    }
    catch (Node.NodeEx nex)
    {
        Console.WriteLine($"Edge wasn't added: {nex.Message}");
    }

}

void show()
{
    foreach (var node in graph.Nodes)
    {
        Console.WriteLine(node.Name + ": ");
        if (node.Edges != null)
            foreach (var edge in node.Edges)
                Console.WriteLine($"{edge.BNode.Name}  -->  {edge.ENode.Name}");
        Console.WriteLine();
    }
}

void test() //Алгоритм Прима
{

    string[] textLines = extractFile().Split('\n');
    foreach (var line in textLines)
    {
        var ndANDed = line.Split(' ');
        var nd = ndANDed[0];
        string from = nd[0].ToString(), end = nd[1].ToString();
        graph.makeNode(from);
        graph.makeNode(end);

        int ed = Convert.ToInt32(ndANDed[1]);

        graph.addEdge(from, end, ed);
    }
}


do
{
    Console.Clear();
    Console.WriteLine(CONSOLE_TEXT);
    input = Console.ReadLine().ToUpper();

    switch (input) {
        case "E":
            tryAddEdge();
            break;
        case "M":
            Console.Write("Input node name: ");
            string name = Console.ReadLine();

            if (graph.makeNode(name))
                Console.WriteLine("Success!");
            else
                Console.WriteLine($"Node with name \"{name}\" already exists!");

            break;
        case "F":
            if (graph.Nodes.Count > 0)
            {
                foreach (var edge in graph.findMinTree())
                {
                    Console.WriteLine("{0}  {1}-->  {2}", edge.BNode.Name, edge.Weight, edge.ENode.Name);
                }
                
            }
            else
                Console.WriteLine("Graph is empty!");
            break;
        case "S":
            if (graph.Nodes.Count > 0)
                show();
            else Console.WriteLine("Graph is empty!");
            break;
        case "T":
            test();
            break;
        case "N":
            break;
    }
    if (input != "N")
        Console.ReadLine();
}
while (input != "N");